package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.IsNewVisitorTypeStats;
import com.atguigu.gmall.publisher.service.IsNewVisitorTypeStatsServince;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class IsNewVisitorTypeStatsServincecontroller {
    @Autowired
    private IsNewVisitorTypeStatsServince isNewVisitorTypeStatsServince;

    @RequestMapping("/IsNewVisitorT")
    public String getVisitorPerType(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<IsNewVisitorTypeStats> visitorTypeStatsList = isNewVisitorTypeStatsServince.getVisitorTypeStats(date);
        System.out.println("size" + visitorTypeStatsList.size());
        if (visitorTypeStatsList == null || visitorTypeStatsList.size() == 0) {
            return "无数据";
        }
        // 方法二，直接拼接字符串，简单明了
        IsNewVisitorTypeStats newVisitorStats = null;
        IsNewVisitorTypeStats oldVisitorStats = null;
        for (IsNewVisitorTypeStats visitorStats : visitorTypeStatsList) {
            if ("1".equals(visitorStats.getIsNew())) {
                // 新访客
                newVisitorStats = visitorStats;
            } else {
                // 老访客
                oldVisitorStats = visitorStats;
            }
        }
        System.out.println("212");
        //拼接json字符串
        String json = "{\"status\":0,\"data\":{\"total\":5," +
                "\"columns\":[" +
                "{\"name\":\"类别\",\"id\":\"type\"}," +
                "{\"name\":\"新访客\",\"id\":\"new\"}," +
                "{\"name\":\"老访客\",\"id\":\"old\"}]," +
                "\"rows\":[" +
                "{\"type\":\"访客数(人)\",\"new\":" + newVisitorStats.getUvCt() + ",\"old\":" + oldVisitorStats.getUvCt() + "}," +
                "{\"type\":\"总访问页面数(次)\",\"new\":" + newVisitorStats.getPvCt() + ",\"old\":" + oldVisitorStats.getPvCt() + "}," +
                "{\"type\":\"跳出率(%)\",\"new\":" + newVisitorStats.getUjRate() + ",\"old\":" + oldVisitorStats.getUjRate() + "}," +
                "{\"type\":\"平均在线时长(秒)\",\"new\":" + newVisitorStats.getAvgDurSum() + ",\"old\":" + oldVisitorStats.getAvgDurSum() + "}," +
                "{\"type\":\"平均访问页面数(人次)\",\"new\":" + newVisitorStats.getAvgPvCt() + ",\"old\":" + oldVisitorStats.getAvgPvCt() + "}]}}";

        return json;

    }
}
